<?php
/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */
namespace Admin\Controller;
use Think\AjaxPage;
use Think\Page;
use Admin\Model\UserModel;
class UserController extends BaseController {
    public function index() {
        $this->display();
    }
    /**
     * 会员列表
     */
    public function ajaxIndex() {
        $condition = array();
        I('phone') ? $condition['phone'] = I('phone') : false;
        I('email') ? $condition['email'] = I('email') : false;
        I('user_id') ? $condition['id'] = I('user_id') : false;
        I('nickname') ? $condition['user_name'] = I('nickname') : false;
        $sort_order = 'id desc';
        $model = M('users');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $userList = $model->where($condition)->order($sort_order)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $show = $Page->show();
        $this->assign('userList', $userList);
        $this->assign('page', $show);
        $this->display();
    }
	
    /**
     * 会员详细信息查看
     */
    public function detail() {
        $uid = I('get.id');
        $user = D('users')->where(array('id' => $uid))->find();
        if (!$user)
            exit($this->error('会员不存在'));
        if (IS_POST) {
			$check = M('users')->where(array('id'=>array('neq',$uid),'phone'=>$_POST['phone']))->count();
			if($check){
				exit($this->error('该用户已存在'));
			}
            //  会员信息编辑
            $password = I('post.password');
            $password2 = I('post.password2');
            if ($password != '' && $password != $password2) {
                exit($this->error('两次输入密码不同'));
            }
            if ($password == '' && $password2 == '') {
                unset($_POST['password']);
            } else {
                $_POST['password'] = encrypt($_POST['password']);
            }
            $row = M('users')->where(array('id' => $uid))->save($_POST);
            if ($row)
                exit($this->success('修改成功'));
            exit($this->error('未作内容修改或修改失败'));
        }
        $this->assign('user', $user);
        $this->display();
    }
	//余额明细
	public function balance(){
		$id = I('id')?I('id'):'';
		$list = M('users_log')->where(array('user_id'=>$id))->select();
		if(count($list)<1){
			$this->error('暂无明细',U('User/index/'));
		}
		$this->assign('list', $list);
		$this->display();
	}
    public function addUser() {
        if (IS_POST) {
            $data = I('post.');
			if (!$data['user_name']) {
                exit($this->error('请输入昵称'));
            }
            $user_obj = new UserModel();
            $res = $user_obj->addUser($data);
            if ($res['status'] == 1) {
                $this->success('添加成功', U('User/index'));
                exit;
            } else {
                $this->error('添加失败,' . $res['msg'], U('User/index'));
            }
        }
        $this->display();
    }
    /**
     * 用户收货地址查看
     */
    public function address() {
        $uid = I('get.id');
        $lists = D('user_addr')->where(array('user_id' => $uid))->select();
        $regionList = M('Region')->getField('id,name');
        $this->assign('regionList', $regionList);
        $this->assign('lists', $lists);
        $this->display();
    }
    /**
     * 删除会员
     */
    public function freeze() {
        $uid = I('id');
		$status = M('users')->where(array('id' => $uid))->getField('status');
        $row = M('users')->where(array('id' => $uid))->save(array('status'=>($status+1)%2));
        if ($row) {			
            echo json_encode(array('code'=>200,'status'=>($status+1)%2));
        } else {
            echo json_encode(array('code'=>400));
        }
    }
	public function thaw() {
        $uid = I('id');
        $row = M('users')->where(array('id' => $uid))->save(array('status'=>1));
        if ($row) {			
            echo json_encode(array('code'=>200));
        } else {
            echo json_encode(array('code'=>400));
        }
    }
    /**
     * 账户资金记录
     */
    public function userLog() {
        $user_id = I('get.id');
        //获取类型
        $type = I('get.type');
        //获取记录总数
        $count = M('user_log')->where(array('user_id' => $user_id))->count();
        $page = new Page($count);
        $lists = M('user_log')->where(array('user_id' => $user_id))->order('change_time desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('user_id', $user_id);
        $this->assign('page', $page->show());
        $this->assign('lists', $lists);
        $this->display();
    }
    /**
     * 账户资金调节
     */
    public function userLogEdit() {
        $user_id = I('get.id');
        if (!$user_id > 0)
            $this->error("参数有误");
        if (IS_POST) {
            //获取操作类型
            $m_op_type = I('post.money_act_type');
            $user_money = I('post.user_money');
            $user_money = $m_op_type ? $user_money : 0 - $user_money;
            $p_op_type = I('post.point_act_type');
            $pay_points = I('post.pay_points');
            $pay_points = $p_op_type ? $pay_points : 0 - $pay_points;
            $f_op_type = I('post.frozen_act_type');
            $frozen_money = I('post.frozen_money');
            $frozen_money = $f_op_type ? $frozen_money : 0 - $frozen_money;
            $desc = I('post.desc');
            if (!$desc)
                $this->error("请填写操作说明");
            if (userLog($user_id, $user_money, $pay_points, $desc)) {
                $this->success("操作成功", U("Admin/User/userLog", array('id' => $user_id)));
            } else {
                $this->error("操作失败");
            }
            exit;
        }
        $this->assign('user_id', $user_id);
        $this->display();
    }
    public function level() {
        $act = I('GET.act', 'add');
        $this->assign('act', $act);
        $level_id = I('GET.level_id');
        $level_info = array();
        if ($level_id) {
            $level_info = D('user_level')->where('level_id=' . $level_id)->find();
            $this->assign('info', $level_info);
        }
        $this->display();
    }
    public function levelList() {
        $Ad = M('user_level');
        $res = $Ad->where('1=1')->order('level_id')->page($_GET['p'] . ',10')->select();
        if ($res) {
            foreach ($res as $val) {
                $list[] = $val;
            }
        }
        $this->assign('list', $list);
        $count = $Ad->where('1=1')->count();
        $Page = new \Think\Page($count, 10);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->display();
    }
    public function levelHandle() {
        $data = I('post.');
        if ($data['act'] == 'add') {
            $r = D('user_level')->add($data);
        }
        if ($data['act'] == 'edit') {
            $r = D('user_level')->where('level_id=' . $data['level_id'])->save($data);
        }
        if ($data['act'] == 'del') {
            $r = D('user_level')->where('level_id=' . $data['level_id'])->delete();
            if ($r)
                exit(json_encode(1));
        }
        if ($r) {
            $this->success("操作成功", U('Admin/User/levelList'));
        } else {
            $this->error("操作失败", U('Admin/User/levelList'));
        }
    }
    /**
     * 搜索用户名
     */
    public function searchUser() {
        $search_key = trim(I('search_key'));
        if (strstr($search_key, '@')) {
            $list = M('users')->where(" email like '%$search_key%' ")->select();
            foreach ($list as $key => $val) {
                echo "<option value='{$val['user_id']}'>{$val['email']}</option>";
            }
        } else {
            $list = M('users')->where(" mobile like '%$search_key%' ")->select();
            foreach ($list as $key => $val) {
                echo "<option value='{$val['user_id']}'>{$val['mobile']}</option>";
            }
        }
        exit;
    }
	
	public function friend(){
		$id = $_REQUEST['id']?$_REQUEST['id']:'';
		$mod = M('users_friend');
		$users_mod = M('users');
		$res = $list = array();
        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = empty($_REQUEST['size']) ? 20 : $_REQUEST['size'];
        $where['user_id'] = $id;
        $res = $mod->where($where)->page("$p,$size")->select();	
		foreach((array)$res as $k=>$v){
			$res[$k]['user'] = $users_mod->where(array('id'=>$v['friend_id']))->field('user_name,head_picture')->find();
		}
        $count = $mod->where($where)->count();
		if(!$count){
			 $this->error("暂无好友", U('Admin/User/index'));
		}
        $pager = new \Think\Page($count, $size);
        $page = $pager->show();
        $this->assign('list', $res);
        $this->assign('page', $page);
        $this->display();
	}
}
